home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
machack
/
Hacks97
/
WarriorsProgress.sit
/
Warrior’s Progress
/
source code
/
Source
/
Libraries
/
Strings
/
Numeral.cp
< prev
next >
Wrap
Text File
|
1997-06-28
|
3KB
|
164 lines
// Numeral.cp
#ifndef Numeral_h
#include "Numeral.h"
#endif
#ifndef Assert_h
#include "Assert.h"
#endif
uint8 Numeral::Sign( int32 value )
{
return ( value < 0 )
? minus
: ( value == 0 ) ? none : plus;
}
uint8 Numeral::Sign( uint32 value )
{
return ( value == 0 ) ? none : plus;
}
Numeral::Numeral( uint64 value, uint32 base )
: sign( Sign( value ) ),
showSign( 0 ),
firstDigit( digits + maxLength )
{
MakeDigits64( value, base );
}
Numeral::Numeral( int64 value, uint32 base )
: sign( Sign( value ) ),
showSign( value < 0 ),
firstDigit( digits + maxLength )
{
if ( value < 0 )
value = - value;
MakeDigits64( value, base );
}
Numeral::Numeral( uint32 value, uint32 base )
: sign( Sign( value ) ),
showSign( 0 ),
firstDigit( digits + maxLength )
{
MakeDigits( value, base );
}
Numeral::Numeral( int32 value, uint32 base )
: sign( Sign( value ) ),
showSign( value < 0 ),
firstDigit( digits + maxLength )
{
if ( value < 0 )
value = - value;
MakeDigits( value, base );
}
Numeral::Numeral( uint16 value, uint32 base )
: sign( Sign( uint32( value ) ) ),
showSign( 0 ),
firstDigit( digits + maxLength )
{
MakeDigits( value, base );
}
Numeral::Numeral( int16 value, uint32 base )
: sign( Sign( int32( value ) ) ),
showSign( value < 0 ),
firstDigit( digits + maxLength )
{
if ( value < 0 )
value = - value;
MakeDigits( value, base );
}
Numeral::Numeral( uint8 value, uint32 base )
: sign( Sign( uint32( value ) ) ),
showSign( 0 ),
firstDigit( digits + maxLength )
{
MakeDigits( value, base );
}
Numeral::Numeral( int8 value, uint32 base )
: sign( Sign( int32( value ) ) ),
showSign( value < 0 ),
firstDigit( digits + maxLength )
{
if ( value < 0 )
value = - value;
MakeDigits( value, base );
}
uint8 Numeral::Digit( uint32 n )
{
Assert( n < 36 );
if ( n < 10 )
return '0' + n;
return 'a' + n - 10;
}
void Numeral::MakeDigits( uint32 value, uint32 base )
{
Assert( base > 1 );
Assert( base <= 36 );
do
{
*--firstDigit = Digit( value % base );
value /= base;
}
while( value > 0 );
}
void Numeral::PadTo( uint32 minDigits )
{
Assert( minDigits <= maxLength - 2 );
while ( DigitCount() < minDigits )
*--firstDigit = '0';
}
Numeral::operator ConstStr255Param() const
{
uint8 *stringStart = firstDigit;
if ( showSign )
*--stringStart = sign;
uint32 length = digits + maxLength - stringStart;
*--stringStart = length;
return stringStart;
}
uint8 Numeral::Sign( int64 value )
{
return ( value < 0 )
? minus
: ( value == 0 ) ? none : plus;
}
uint8 Numeral::Sign( uint64 value )
{
return ( value == 0 ) ? none : plus;
}
void Numeral::MakeDigits64( uint64 value, uint32 base )
{
Assert( base > 1 );
Assert( base <= 36 );
do
{
*--firstDigit = Digit( value % base );
value /= base;
}
while( value > 0 );
}